home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / LOAD_SITE.PY < prev    next >
Encoding:
Python Source  |  2000-02-02  |  11.2 KB  |  367 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. """Load a Zope site from a collection of files or directories
  65. """
  66.  
  67. usage=""" [options] url file .....
  68.  
  69.     where options are:
  70.  
  71.       -D
  72.  
  73.          For HTML documents, replace the start of the content, up to
  74.          and including the opening body tag with a DTML var tag that
  75.          inserts the standard header. Also replace the closing body
  76.          and html tag with a DTML var tag that inserts the standard
  77.          footer.
  78.  
  79.       -I
  80.  
  81.          For each index.html, add an index_html that redirects.
  82.  
  83.       -p path
  84.  
  85.          Path to ZPublisher.  If not provided, load_site will
  86.          make an attempt to figure it out.
  87.  
  88.       -u user:password
  89.  
  90.          Credentials
  91.  
  92.       -v
  93.  
  94.          Run in verbose mode.
  95.  
  96.       -9
  97.  
  98.          Use *old* zope method names.
  99. """
  100.  
  101. import sys, getopt, os, string
  102. ServerError=''
  103. verbose=0
  104. old=0
  105. doctor=0
  106. index_html=0
  107.  
  108. def main():
  109.     user, password = 'superuser', '123'
  110.     opts, args = getopt.getopt(sys.argv[1:], 'p:u:DIv9')
  111.     global verbose
  112.     global old
  113.     global doctor
  114.     global index_html
  115.     havepath=None
  116.     for o, v in opts:
  117.         if o=='-p':
  118.             d, f = os.path.split(v)
  119.             if f=='ZPublisher': sys.path.insert(0,d)
  120.             else: sys.path.insert(0,v)
  121.             havepath=1
  122.         elif o=='-u':
  123.             v = string.split(v,':')
  124.             user, password = v[0], string.join(v[1:],':')
  125.         elif o=='-D': doctor=1
  126.         elif o=='-I': index_html=1
  127.         elif o=='-v': verbose=1
  128.         elif o=='-9': old=1
  129.  
  130.     if not args:
  131.         print sys.argv[0]+usage
  132.         sys.exit(1)
  133.  
  134.     if not havepath:
  135.         here=os.path.split(sys.argv[0])[0]
  136.         if os.path.exists(os.path.join(here,'ZPublisher')):
  137.             sys.path.insert(0,here)
  138.         else:
  139.             here=os.path.split(here)[0]
  140.             here=os.path.join(here,'lib','python')
  141.             if os.path.exists(os.path.join(here,'ZPublisher')):
  142.                 sys.path.insert(0,here)
  143.         
  144.  
  145.     url=args[0]
  146.     files=args[1:]
  147.  
  148.     import ZPublisher.Client
  149.     global ServerError
  150.     ServerError=ZPublisher.Client.ServerError
  151.     object=ZPublisher.Client.Object(url, username=user, password=password)
  152.  
  153.     for f in files: upload_file(object, f)
  154.  
  155. def call(f, *args, **kw):
  156.     # Call a function ignoring redirect bci errors.
  157.     try: apply(f,args, kw)
  158.     except ServerError, v:
  159.         if str(v)[:1] != '3':
  160.             raise sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]
  161.  
  162. def upload_file(object, f):
  163.     if os.path.isdir(f): return upload_dir(object, f)
  164.     dir, name = os.path.split(f)
  165.     root, ext = os.path.splitext(name)
  166.     if ext in ('file', 'dir'): ext=''
  167.     else:
  168.         ext=string.lower(ext)
  169.         if ext and ext[0] in '.': ext=ext[1:]
  170.     if ext and globals().has_key('upload_'+ext):
  171.         if verbose: print 'upload_'+ext, f
  172.         return globals()['upload_'+ext](object, f)
  173.  
  174.     if verbose: print 'upload_file', f, ext
  175.     call(object.manage_addFile, id=name, file=open(f,'rb'))
  176.  
  177. def upload_dir(object, f):
  178.     if verbose: print 'upload_dir', f
  179.     dir, name = os.path.split(f)
  180.     call(object.manage_addFolder, id=name)
  181.     object=object.__class__(object.url+'/'+name,
  182.                             username=object.username,
  183.                             password=object.password)
  184.     for n in os.listdir(f):
  185.         upload_file(object, os.path.join(f,n))
  186.  
  187. # ----- phd -----
  188. # Modified by Oleg Broytmann <phd2@earthling.net>
  189.  
  190. from sgmllib import SGMLParser
  191.  
  192. def join_attrs(attrs):
  193.    attr_list = []
  194.    for attrname, value in attrs:
  195.       attr_list.append('%s="%s"' % (attrname, string.strip(value)))
  196.  
  197.    if attr_list:
  198.       s = " " + string.join(attr_list, " ")
  199.    else:
  200.       s = ""
  201.  
  202.    return s
  203.  
  204.  
  205. class HeadParser(SGMLParser):
  206.    def __init__(self):
  207.       SGMLParser.__init__(self)
  208.  
  209.       self.seen_starthead = 0
  210.       self.seen_endhead   = 0
  211.       self.seen_startbody = 0
  212.  
  213.       self.head = ""
  214.       self.title = ""
  215.       self.accumulator = ""
  216.  
  217.  
  218.    def handle_data(self, data):
  219.       if data:
  220.          self.accumulator = self.accumulator + data
  221.  
  222.    def handle_comment(self, data):
  223.       if data:
  224.          self.accumulator = self.accumulator + "<!--%s-->" % data
  225.  
  226.  
  227.    def start_head(self, attrs):
  228.       if not self.seen_starthead:
  229.          self.seen_starthead = 1
  230.          self.head = ""
  231.          self.title = ""
  232.          self.accumulator = ""
  233.  
  234.    def end_head(self):
  235.       if not self.seen_endhead:
  236.          self.seen_endhead = 1
  237.          self.head = self.head + self.accumulator
  238.          self.accumulator = ""
  239.  
  240.  
  241.    def start_title(self, attrs):
  242.       self.head = self.head + self.accumulator
  243.       self.accumulator = ""
  244.  
  245.    def end_title(self):
  246.       self.title = self.accumulator
  247.       self.accumulator = ""
  248.  
  249.  
  250.    def start_body(self, attrs):
  251.       if not self.seen_startbody:
  252.          self.seen_startbody = 1
  253.          self.accumulator = ""
  254.  
  255.    def end_body(self): pass # Do not put </BODY> and </HTML>
  256.    def end_html(self): pass # into output stream
  257.  
  258.  
  259.    # Pass other tags unmodified
  260.    def unknown_starttag(self, tag, attrs):
  261.       self.accumulator = self.accumulator + "<%s%s>" % (string.upper(tag), join_attrs(attrs))
  262.  
  263.    def unknown_endtag(self, tag):
  264.       self.accumulator = self.accumulator + "</%s>" % string.upper(tag)
  265.  
  266.  
  267.  
  268. def parse_html(infile):
  269.    parser = HeadParser()
  270.  
  271.    while 1:
  272.       line = infile.readline()
  273.       if not line: break
  274.       parser.feed(line)
  275.  
  276.    parser.close()
  277.    infile.close()
  278.  
  279.    return string.strip(parser.title), string.strip(parser.head), \
  280. """<!--#var standard_html_header-->
  281.  
  282. """ + string.strip(parser.accumulator) + """
  283.  
  284. <!--#var standard_html_footer-->"""
  285.  
  286.  
  287. def upload_html(object, f):
  288.     dir, name = os.path.split(f)
  289.     f=open(f)
  290.  
  291.     if doctor: title, head, body = parse_html(f)
  292.     else:
  293.         if old: f=f.read()
  294.         title, head, body = '', '', f
  295.  
  296.     if old:
  297.         call(object.manage_addDocument, id=name, file=body)
  298.         if index_html and name in ('index.html', 'index.htm'):
  299.             call(object.manage_addDocument, id='index_html',
  300.                  file=('<!--#raise Redirect-->'
  301.                        '<!--#var URL1-->/%s'
  302.                        '<!--#/raise-->' % name
  303.                        ))
  304.     else:
  305.         call(object.manage_addDTMLDocument, id=name, title=title, file=body)
  306.         if index_html and name in ('index.html', 'index.htm'):
  307.             call(object.manage_addDTMLMethod, id='index_html',
  308.                  file=('<dtml-raise Redirect>'
  309.                        '<dtml-var URL1>/%s'
  310.                        '</dtml-raise>' % name
  311.                        ))
  312.  
  313.     # Now add META and other tags as property
  314.     if head:
  315.       object=object.__class__(object.url+'/'+name,
  316.                             username=object.username,
  317.                             password=object.password)
  318.       call(object.manage_addProperty,
  319.            id="loadsite-head", type="text", value=head)
  320.  
  321. # ----- /phd -----
  322.     
  323. upload_htm=upload_html
  324.  
  325. def upload_dtml(object, f):
  326.     dir, name = os.path.split(f)
  327.     f=open(f)
  328.  
  329.     if old:
  330.         f=f.read()
  331.         call(object.manage_addDocument, id=name, file=f)
  332.     else:
  333.         call(object.manage_addDTMLMethod, id=name, file=f)
  334.         
  335.  
  336. def upload_gif(object, f):
  337.     dir, name = os.path.split(f)
  338.     call(object.manage_addImage, id=name, file=open(f,'rb'))
  339.  
  340. upload_jpg=upload_gif
  341. upload_png=upload_gif
  342.  
  343. if __name__=='__main__': main()
  344.  
  345.  
  346.